using System;
using System.Collections.Generic;
using System.Text;
using Barrage;
using Barrage.Meshes;
using Spacedash.Game.Shaders;
using Spacedash.Logic.Scenario;
using Spacedash.Game.Meshes;
using Spacedash.Game.Items;
using Barrage.Textures;
using Spacedash.Logic.Scenario.Cells;

using SlimDX;
using Barrage.Global;
using Barrage.Elements;
using Barrage.Shaders;
using Spacedash.Logic;
namespace Spacedash.Game.Nodes
{
    class ScenarioNode : Node
    {
        const string CellDir = "Cells\\";
        Scenario scenario = null;
        public Scenario Scenario { get { return scenario; } }

        ScenarioItem scenarioItem;
        public ScenarioItem ScenarioItem { get { return scenarioItem; } }

        public ScenarioNode(Scenario scenario)
        {
            this.scenario = scenario;
            loadScenario();
        }

        protected void loadScenario()
        {
            scenarioItem = new ScenarioItem(scenario);
            addElement(scenarioItem);

            Item instanceItem = new Item();

            // Init some mesh data
            foreach (CellData cd in Spacedash.Logic.Scenario.Cells.CellManager.CellData.Values)
            {
                foreach (string name in cd.Meshes)
                {
                    if (!MeshManager.Meshes.Exists("IMesh_" + name))
                    {
                        InstancedMesh instanceMesh = new InstancedMesh(Store.PathConfig[Store.MODEL_PATH] + name);
                        instanceMesh.Shader = ShaderManager.Shaders[CellDir + cd.Shader + ".fx"];
                        instanceMesh.GroupSize = 200;
                        MeshManager.Meshes["IMesh_" + name] = instanceMesh;
                    }
                    ((InstancedMesh)MeshManager.Meshes["IMesh_" + name]).ClearInstances();
                    instanceItem.addMesh(MeshManager.Meshes["IMesh_"+name]);
                }
            }

            for(int i=0;i<scenario.RowNumber;i++)
            {
                for (int j = 0; j < scenario.ColumnNumber; j++)
                {
                    CellData data = scenario.CellAt(i,j).CellData;
                    if (data != null)
                    {
                        foreach (string name in data.Meshes)
                        {
                            Motion m=new Motion();
                            m.Matrix = scenario.getItemMotion(new Vector2(((j) - scenario.ColumnNumber / 2), (i + 0.5f)));
                            m.Scale = new Vector3(scenario.CellSize.X/10.0f,1,scenario.CellSize.Y/10.0f);
                            InstancedMesh instanceMesh = (InstancedMesh)MeshManager.Meshes["IMesh_" + name];
                            instanceMesh.AddInstance(new InstanceData(m.WorldMatrix));
                        }
                    }
                }
            }

            foreach (Mesh m in instanceItem.Meshes)
            {
                ((InstancedMesh)m).UpdateData();
            }

            addElement(instanceItem);
        }
    }
}
